<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>商品进货管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/resources/favicon.ico">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/resources/css/public.css" media="all" />
</head>
<body class="childrenBody">
<!--查询条件开始-->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 5px;">
    <legend>查询条件</legend>
</fieldset>
<blockquote class="layui-elem-quote">
    <form action="/inport/loadAllInport" method="post" id="dataForm" lay-filter="dataForm" class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">供应商</label>
                <div class="layui-input-inline">
                    <select name="providerid" id="search_providerid">
                        <option value="0">请选择供应商</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-inline">
                    <select name="goodsid" id="search_goodsid">
                        <option value="0">请选择商品</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">开始时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="startTime" id="startTime" readonly  class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">操作员</label>
                <div class="layui-input-inline">
                    <input type="text" name="operateperson" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-inline">
                    <input type="text" name="remark" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">结束时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="endTime" id="endTime" readonly  class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center">
            <div class="layui-inline">
                <button type="button" class="layui-btn layui-btn-normal" lay-submit="" lay-filter="doSearch"><span class="layui-icon layui-icon-search"></span>查询</button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh"></span>重置</button>

            </div>
        </div>
    </form>
</blockquote>
<!--查询条件结束-->
<!--数据表格开始-->
<div>
    <table class="layui-hide" id="inportTable" lay-filter="inportTable"></table>
    <div id="inportToolBar" style="display: none">
        <button type="button" lay-event="add" shiro:hasPermission="inport:create" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-add-1"></span>添加</button>
    </div>
    <div id="inportRowBar" style="display: none">
        <button type="button" lay-event="update" shiro:hasPermission="inport:update" class="layui-btn layui-btn-sm layui-btn-warm"><span class="layui-icon layui-icon-edit"></span>修改</button>
        <button type="button" lay-event="delete" shiro:hasPermission="inport:delete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>删除</button>
        <button type="button" lay-event="back" shiro:hasPermission="inport:back" class="layui-btn layui-btn-sm layui-btn-normal"><span class="layui-icon layui-icon-transfer"></span>退货</button>
    </div>
</div>
<!--数据表格结束-->
<!--添加与修改的弹出层开始-->
<div style="display: none;padding: 10px" id="addOrUpdateDiv">
    <form action="" method="post" id="inportForm" class="layui-form layui-form-pane" lay-filter="inportForm">
        <div class="layui-form-item">
            <input type="hidden" name="id"/>
            <div class="layui-inline">
                <label class="layui-form-label">供应商</label>
                <div class="layui-input-inline">
                    <select name="providerid" id="providerid" lay-filter="providerid">
                        <option value="0">请选择供应商</option>
                    </select>
                </div>
<!--                解决select禁用 禁用时无法获取值-->
<!--                1.显示时禁用，提交数据时解除禁用-->
<!--               2.就是在div上加了一个透明的div 添加时隐藏 修改时显示-->
                <div class="mydiv" title="不可修改" style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"> </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-inline">
                    <select name="goodsid" id="goodsid">
                        <option value="0">请选择商品</option>
                    </select>
                </div>
                <div class="mydiv" title="不可修改" style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"> </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">进货数量</label>
                <div class="layui-input-inline">
                    <input type="text" name="number" lay-verify="number" autocomplete="off" placeholder="请输入进货数量" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">进货价格</label>
                <div class="layui-input-inline">
                    <input type="text" name="inportprice" lay-verify="number" autocomplete="off" placeholder="请输入进货价格" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">支付类型</label>
            <div class="layui-input-block">
                <input type="radio" name="paytype" value="微信"  class="layui-input" title="微信" checked>
                <input type="radio" name="paytype" value="支付宝"  class="layui-input" title="支付宝">
                <input type="radio" name="paytype" value="信用卡"  class="layui-input" title="信用卡">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">支付备注</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" name="remark"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center">
                <button type="button" class="layui-btn layui-btn-normal" id="doSubmit" lay-submit="" lay-filter="doSubmit"><span class="layui-icon layui-icon-ok"></span>提交</button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh"></span>重置</button>
            </div>
        </div>
    </form>
</div>
<!--添加与修改的弹出层结束-->
<!--退货弹出层 开始-->
<div id="backGoodsDiv" style="display: none;padding:5px">
    <form action="" method="post" id="inportBackForm" class="layui-form layui-form-pane" lay-filter="inportBackForm">
        <div class="layui-form-item">
            <input type="hidden" name="id"/> <!--退货ID-->
                <label class="layui-form-label">退货数量</label>
                <div class="layui-input-block">
                    <!--进货数量 -->
                    <input id="currentnumber" name="currentnumber" type="hidden"/>
                    <input type="text" id="number" name="number" lay-verify="number|checknumber" class="layui-input">
                </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">退货备注</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" name="remark"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center">
                <button type="button" class="layui-btn layui-btn-normal" id="doBackSubmit" lay-submit="" lay-filter="doBackSubmit"><span class="layui-icon layui-icon-ok"></span>提交</button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh"></span>重置</button>
            </div>
        </div>
    </form>
</div>
<!--退货弹出层 结束-->
<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['jquery','form','table','layer','laydate'],function () {
        var $=layui.jquery;
        var form=layui.form;
        var table=layui.table;
        var layer=layui.layer;
        var laydate=layui.laydate;
        //初始化日期选择器
        laydate.render({
            elem:"#startTime",
            type:"datetime"
        });
        laydate.render({
            elem:"#endTime",
            type:"datetime"
        });
        //表格数据渲染
        var tableIns=table.render({
            elem: '#inportTable'
            ,url:'/inport/loadAllInport'
            ,toolbar: '#inportToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,title: '商品进货数据表'
            ,height: 'full-220'
            ,page: true
            ,cols: [ [
                {field:'id', title:'ID',align: 'center',width:'80'}
                ,{field:'providername', title:'供应商',align: 'center',width:'100'}
                ,{field:'goodsname', title:'商品名称',align: 'center',width:'150'}
                ,{field:'size', title:'商品规格',align: 'center',width:'100'}
                ,{field:'inporttime', title:'进货时间',align: 'center',width:'200'}
                ,{field:'number', title:'进货数量',align: 'center',width:'100'}
                ,{field:'inportprice', title:'进货价格',align: 'center',width:'150'}
                ,{field:'remark', title:'备注',align: 'center',width:'120'}
                ,{field:'operateperson', title:'操作员',align: 'center',width:'120'}
                ,{field:'paytype', title:'支付方式',align: 'center',width:'120'}
                ,{fixed: 'right', title:'操作', toolbar: '#inportRowBar',align: 'center',width:'300'}
            ] ]
            ,done: function(res, curr, count){
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                console.log(res);
                //得到当前页码
                console.log(curr);
                //得到数据总量
                console.log(count);
                //当删除数据表格最后一页的最后一条数据时 渲染上一页的数据
                if(res.data.length==0&&curr!=1){
                    tableIns.reload({
                        page:{
                            curr:(curr-1)
                        }
                    })
                }
            }
        });
        //模糊查询
        form.on("submit(doSearch)",function (data) {
            tableIns.reload({
                where: data.field
                ,page: {
                    curr: 1
                }
            });
            return false;
        });
        //头工具事件
        table.on("toolbar(inportTable)",function(obj){

            switch(obj.event){
                case 'add':
                    addInportLayer();
                    break;
            }
        });
        //行工具监听
        table.on("tool(inportTable)", function(obj){
            var data=obj.data;
            switch(obj.event){
                case 'update':
                    updateInportLayer(data);
                    break;
                case 'delete':
                    deleteInport(data);
                    break;
                case 'back':
                    back(data);
                    break;
            }
        });
        var mainIndex;
        var url;
        //添加弹出层设置
        function addInportLayer() {
            //初始化添加弹出层
            mainIndex=layer.open({
                type: 1,
                title:'添加商品进货',
                content: $('#addOrUpdateDiv'),
                area:['700px','450px'],
                success: function () {
                    //重置
                    $('#inportForm')[0].reset();
                    url='/inport/addInport';
                    initSelectProviderName();
                    initSelectGoodsName();
                    $('.mydiv').hide();
                }
            });
        }
        //修改弹出层设置
        function updateInportLayer(data){
            //初始化添加弹出层
            mainIndex=layer.open({
                type: 1,
                title:'修改商品进货',
                content: $('#addOrUpdateDiv'),
                area:['800px','550px'],
                success: function () {
                    //重置
                    $('#inportForm')[0].reset();
                    //表单加载数据
                    form.val('inportForm',data);
                    url='/inport/updateInport';
                    //初始化商品和供应商
                    initSelectProviderName(data.providerid);
                    initSelectGoodsName(data.providerid,data.goodsid);
                    $(".mydiv").show();
                }
            });
        }
        //监听提交
        form.on("submit(doSubmit)",function (data) {
            $.post(url,data.field,function (res) {
                if(res.code==200){
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            });
        });
        //删除函数
        function deleteInport(data) {
            layer.confirm('你确定要删除这个商品进货吗？', {icon: 3, title:'提示'}, function(index){
                $.post("/inport/deleteInport",{id:data.id},function(res){
                    if(res.code==200){
                        tableIns.reload();
                    }
                    layer.msg(res.msg);
                });
                layer.close(index);
            });
        }

        //渲染查询条件的供应商名称
        $.get("/provider/loadAllProviderName",function (res) {
            var data=res.data;
            var html="<option value='0'>请选择供应商</option>";
            var dom=$("#search_providerid");
            $.each(data,function(index,item){
                html+="<option value='"+item.id+"'>"+item.providername+"</option>"
            });
            dom.html(html);
            form.render("select");
        });
        //渲染查询条件的商品信息
        $.get("/goods/loadAllGoodsName",function (res) {
            var data=res.data;
            var html="<option value='0'>请选择商品</option>";
            var dom=$("#search_goodsid");
            $.each(data,function(index,item){
                html+="<option value='"+item.id+"'>"+item.goodsname+'-'+item.size+"["+item.providername+"]"+"</option>"
            });
            dom.html(html);
            form.render("select");
        });
        //渲染添加和修改的供应商和商品信息
        function initSelectProviderName(providerid) {
            //每次将商品下拉选择框初始化为空
            // var dom=$("#goodsid");
            // dom.html("");
            $.get("/provider/loadAllProviderName",function (res) {
                var data=res.data;
                var html="<option value='0'>请选择供应商</option>";
                var dom=$("#providerid");
                $.each(data,function(index,item){
                    html+="<option value='"+item.id+"'>"+item.providername+"</option>"
                });
                dom.html(html);
                //如果不为空则反选
                if (providerid!=undefined)
                    dom.val(providerid);
                form.render("select");
            });
        }
        //监听供应商的下拉选项框 动态刷新商品的信息
        form.on('select(providerid)', function(data){
            var providerid=data.value;
           initSelectGoodsName(providerid);
        });
        function initSelectGoodsName(providerid,goodsid) {
            $.get("/goods/loadAllGoodsNameByProviderId",{providerid:providerid},function (res) {
                var data=res.data;
                var html="<option value='0'>请选择商品</option>";
                var dom=$("#goodsid");
                $.each(data,function(index,item){
                    html+="<option value='"+item.id+"'>"+item.goodsname+'-'+item.size+"</option>"
                });
                dom.html(html);
                if (goodsid!=undefined)
                    dom.val(goodsid);
                form.render("select");
            });
        }


        //退货弹出层
        function back(data) {
            mainIndex=layer.open({
                type: 1,
                title:'退货',
                content: $('#backGoodsDiv'),
                area:['800px','450px'],
                success: function () {
                    //重置
                    $('#inportBackForm')[0].reset();
                    //表单加载数据
                    form.val('inportBackForm',{id:data.id,currentnumber:data.number});
                }
            });
        }
        //监听退货的事件
        form.on("submit(doBackSubmit)",function (data) {
            // alert(data.id+"--"+data.number+"--"+data.remark);
            $.post("/outport/addOutport",data.field,function (res) {
                if(res.code==200){
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            })
        });
        //自定义验证规则
        form.verify({
            checknumber: function(value){
                var number=parseInt($("#currentnumber").val());
                if(parseInt(value)>number||parseInt(value)<1){
                    return '退货数量在【0】-【'+number+"】之间";
                }
            }
        });
    })
</script>
</body>
</html>